#include "face_mood_reader.hpp"
#include <utils/ocv_common.hpp>


FaceMoodReader::FaceMoodReader (InferenceEngine::Core &ie, const std::string &device, const std::string &modelDir) {
    auto network = ie.ReadNetwork(modelDir + "/emotions-recognition-retail-0003.xml");
    auto input = network.getInputsInfo();
    auto inputInfo = input.begin()->second;
    inputInfo->setPrecision(InferenceEngine::Precision::U8);
    inputInfo->setLayout(InferenceEngine::Layout::NCHW);
    auto output = network.getOutputsInfo();
    auto outputInfo = output.begin()->second;
    outputInfo->setPrecision(InferenceEngine::Precision::FP32);
    outputInfo->setLayout(InferenceEngine::TensorDesc::getLayoutByDims(outputInfo->getDims()));
    net = ie.LoadNetwork(network, device);
}

auto FaceMoodReader::infer (const cv::Mat &frame) -> Result {
    auto req = net.CreateInferRequest();
    auto blob = req.GetBlob("data");
    matU8ToBlob<uint8_t>(frame, blob);

    req.Infer();
    Result res;
    auto data = InferenceEngine::as<InferenceEngine::MemoryBlob>(req.GetBlob("prob_emotion"))->rmap().as<float*>();
    float max = 0.0;
    int idx = -1;
    for (auto i = 0; i < MoodCount; ++i) {
        if (data[i] > max) {
            max = data[i];
            idx = i;
        }
        res.conf[i] = data[i];
    }
    res.index = idx;
    return res;
}

const char *FaceMoodReader::MoodNames[] = {"正常", "快乐", "悲伤", "惊讶", "愤怒"};
